package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.SettingsMapper;
import com.tanhua.common.pojo.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Philcan
 * @version 1.0
 * @description: TODO
 * @date 2021/5/27 23:54
 */
@Service
public class SettingsService {
    @Autowired
    private SettingsMapper settingsMapper;

    public Settings querySettings(Long userId) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.settingsMapper.selectOne(queryWrapper);
    }

    public void updateNotification(Long id, Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);

        Settings settings = this.settingsMapper.selectOne(queryWrapper);
        if (null == settings) {
            settings = new Settings();
            settings.setUserId(id);
            this.settingsMapper.insert(settings);
        } else {
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            this.settingsMapper.update(settings, queryWrapper);
        }
    }
}
